package com.powernode.controller;

import com.powernode.service.UserService;
import com.powernode.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "短信接口管理")
@RequestMapping("/p/sms")
@RestController
public class SendController {
    @Autowired
    private UserService userService;

    @Value("${aliMsg.regionId}")
    private String regionId;

    @Value("${aliMsg.accessKeyId}")
    private String accessKeyId;

    @Value("${aliMsg.secret}")
    private String secret;

    @Value("${aliMsg.signName}")
    private String signName;

    @Value("${aliMsg.templateCode}")
    private String templateCode;

    @ApiOperation("获取短信验证码")
    @PostMapping("/send")
    public ResponseEntity<String> sendPhoneCode(@RequestBody Map<String, Object> map) {
        String userId = AuthUtil.getLoginUserId();
        map.put("userId", userId);
        map.put("regionId", regionId);
        map.put("accessKeyId", accessKeyId);
        map.put("secret", secret);
        map.put("signName", signName);
        map.put("templateCode", templateCode);
        userService.send(map);
        return ResponseEntity.ok("发送成功！");
    }

    @ApiOperation("绑定用户手机号")
    @PostMapping("/savePhone")
    public ResponseEntity<Void> bindUserPhone(@RequestBody Map<String, Object> map) {
        String userId = AuthUtil.getLoginUserId();
        map.put("userId", userId);
        userService.bindUserPhone(map);
        return ResponseEntity.ok().build();
    }
}
